﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum SceneState {
    Starting,
    NewGame,
    Gaming,
    Finish,
    Gameover,
}

public enum LoadingType {
    Starting2Prepare,
    Prepare2Gaming,
    Prepare2Starting,
    Gaming2Starting,
    Gaming2Finish,
    Finsih2Starting,
}

public class Control : MonoBehaviour {

    [Header("绑定")]
    public _TriggerEventManager _TriggerEventManager;
    public _EventManager _EventManager;

    [Header("runtime")]
    private SceneState sceneState;
    private readonly Dictionary<LoadingType, SceneState> toSceneDic = new Dictionary<LoadingType, SceneState> {
        {LoadingType.Starting2Prepare,      SceneState.NewGame },
        {LoadingType.Prepare2Gaming,        SceneState.Gaming },
        {LoadingType.Prepare2Starting,      SceneState.Starting },
        {LoadingType.Gaming2Starting,       SceneState.Starting },
        {LoadingType.Gaming2Finish,         SceneState.Finish },
        {LoadingType.Finsih2Starting,       SceneState.Starting },
    };


    // Start is called before the first frame update
    void Start() {
        _EventManager.__AttachEvent("Prepare", Prepare);
        _EventManager.__AttachEvent("Game", Game);
        _EventManager.__AttachEvent("Finish", Finish);
        _EventManager.__AttachEvent("Starting", Starting);

    }

    private void Update() {
        CheckShortCut();
    }

    private void ResolutionChange(object o) {
        SetResolution(GlobalData.config.resolution, GlobalData.config.fullScreen);
    }

    private void FullScreenChange(object o) {
        SetResolution(GlobalData.config.resolution, GlobalData.config.fullScreen);
    }

    private void SetResolution(Resolution resolution, bool fullScreen) {
        FullScreenMode fullScreenMode = FullScreenMode.Windowed;
        if (fullScreen) {
            fullScreenMode = FullScreenMode.FullScreenWindow;
        }
        else {
            fullScreenMode = FullScreenMode.Windowed;
        }
        switch (resolution) {
            case Resolution._1920x1080:
                Screen.SetResolution(1920, 1080, fullScreenMode);
                break;

            case Resolution._1200x675:
                Screen.SetResolution(1200, 675, fullScreenMode);
                break;

            case Resolution._1440x810:
                Screen.SetResolution(1440, 810, fullScreenMode);
                break;

            case Resolution._1600x900:
                Screen.SetResolution(1600, 900, fullScreenMode);
                break;
        }
    }

    private void Continue(object o) {
        //StartCoroutine(Loading(LoadingType.Starting2Gaming));
    }

    private void Game(object o) {
        StartCoroutine(Loading(LoadingType.Prepare2Gaming));
    }

    private void Prepare(object o) {
        StartCoroutine(Loading(LoadingType.Starting2Prepare));
    }

    private void Finish(object o) {
        StartCoroutine(Loading(LoadingType.Gaming2Finish));
    }

    private void Starting(object o) {
        switch (sceneState) {
            case SceneState.NewGame:
                StartCoroutine(Loading(LoadingType.Prepare2Starting));
                break;

            case SceneState.Gaming:
                StartCoroutine(Loading(LoadingType.Gaming2Starting));
                break;

            case SceneState.Finish:
                StartCoroutine(Loading(LoadingType.Finsih2Starting));
                break;

        }
    }


    private IEnumerator Loading(LoadingType loadingType) {
        GlobalData.loading = true;
        _TriggerEventManager.TriggerEvent("LoadingStart", loadingType);
        yield return new WaitForSeconds(GlobalData.loadingStartTime);
        GlobalData.sceneState = toSceneDic[loadingType];
        _TriggerEventManager.TriggerEvent("LoadingStable", loadingType);
        _TriggerEventManager.TriggerEvent("LoadingEnd", loadingType);
        yield return new WaitForSeconds(GlobalData.loadingEndTime);
        _TriggerEventManager.TriggerEvent("LoadingFinish", loadingType);
        sceneState = toSceneDic[loadingType];

    }

    private void CheckShortCut() {

    }
}
